<?
/*
whmtools - easily find accounts on cpanels servers
Copyright (C) 2006  Matthew Davis <matthew@familycampground.org>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

include 'header.php';
include 'config.php';
?>
<html>
<head><title>Accounts by Network & Server</title></head>
<link rel="stylesheet" type="text/css" href="main.css">
<?php
$result = mysql_query("SHOW TABLE STATUS FROM `whmtools` LIKE 'accounts'");
$data = mysql_fetch_row($result);
print "Data last updated: " . date("j M y g:i:s",strtotime($data[11])). "<br>";
?>
<a href="updatedata.php">Update data</a> (can take a little while), please be patient<br><br>
<form action="search.php" method="post">
    <input type="text" size=50 name=searchFor>
    <select name="searchBy">
        <option value="domain">Domain</option>
        <option value="server">Server</option>
        <option value="account">Username</option>
        <option value="owner">Owner</option>
        <option value="package">Package</option>
	<option value="realIP">IP</option>
	<option value="skin">Skin</option>
	<option value="quota">Quota</option>
	<option value="dateCreated">Date Created</option>
    </select>
    <input type="submit" value="Search">
    <br><i>Note: Use % as a wildcard.
    <br>Ex: To find all domains with 'ivalue' in it, use '%ivalue%' and select domain.</i>
</form>
<?php

$netQuery = mysql_query("SELECT DISTINCT network FROM servers ORDER BY network");
// Seperate into groups by network
while ($networks = mysql_fetch_row($netQuery)) {
    print "\t<h3>" . $networks[0] . "</h3>\n";
    $sql = "SELECT hostname,ip from `servers` WHERE `network` = '$networks[0]' ORDER BY `hostname`";
    $query = mysql_query($sql);
    print "\t<table border=1>\n\t\t<tr><th>Server Name/<br>WHM Version</th><th>IP Address</th><th>Number of<br>Accounts / Resellers</th><th>WHM<br>Link</th></tr>\n";
    // by server
    while ($servers = mysql_fetch_row($query)) {
	$result = mysql_query("SELECT count('account') FROM `accounts` WHERE `server` = '$servers[0]'");
	$numOfAccounts = mysql_result($result,0);
	$result = mysql_query("SELECT count(DISTINCT owner) FROM `accounts` WHERE `server` = '$servers[0]'");
	// Don't count the reseller 'root' which is on every server
	$resellers = mysql_result($result,0) - 1;
	// Update the WHM version
	$result = mysql_query("SELECT whmVersion FROM `servers` WHERE `hostname` = '$servers[0]'");
	$whmVersion = mysql_result($result,0);
	$whmVersion = preg_replace("'(\r|\n)'","",$whmVersion);

	
	print "\t\t<tr><td><a href=\"search.php?searchBy=server&amp;searchFor=$servers[0]\">$servers[0]</a> / $whmVersion</td>
		<td align=center>$servers[1]</td>
		<td align=center>$numOfAccounts / $resellers</td>
		<td align=center><a href=\"http://$servers[1]:2086\">WHM</a></td></tr>\n";
	$total = $total + $numOfAccounts;
	$resellerNum = $resellerNum + $resellers;
    }
    print "\t</table>\n";
}
mysql_close($dbh);
print "<br>\n";
print "Total Accounts: <strong>" . $total . "</strong><br>\n";
print "Total Resellers: <strong>" . $resellerNum . "</strong><br>\n";
?>
</html>
